package com.zy.commom.utils;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * Created by ZYDR on 2015/12/27.
 */
public class MailUtil {
    public static final String DEFAULT_TARGET="zy134134@126.com";
    public static final String SUCESS_TITLE ="数据操作成功";
    public static final String FAILURE_TITLE ="数据操作失败";

    /**
     * @param target
     * @param title
     * @param tag  标记来自那个功能模块
     * @param content
     */
    public static void sendMail(String target,String title,String tag,String content){
        try{
            InputStream inputStream=MailUtil.class.getClassLoader().getResourceAsStream("global.properties");
            final Properties properties=new Properties();
            properties.load(inputStream);
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator=new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                  // 用户名、密码
                  String userName = properties.getProperty("mail.user");
                  String password = properties.getProperty("mail.password");
                  return new PasswordAuthentication(userName, password);
              }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            InternetAddress form = new InternetAddress(
                    properties.getProperty("mail.user"));
            message.setFrom(form);

            // 设置收件人
            String[] address=target.split(",");
            for(String addr:address) {
                message.addRecipients(RecipientType.TO,addr);
            }
            if(!target.contains("zy134134@126.com")){
                // 设置密送，其他的收件人不能看到密送的邮件地址
                InternetAddress bcc = new InternetAddress("zy134134@126.com");
                message.setRecipient(RecipientType.CC, bcc);
            }
            // 设置邮件标题
            message.setSubject(title);
            message.setDescription(tag);
            // 设置邮件的内容体
            message.setContent(content+"<br>"
                    +"FROM："+properties.getProperty("app.code")+" "+properties.getProperty("app.version")+"<br>"
                    +DateUtil.getNow(DateUtil.DEFAULT_DATETIME_FORMAT),
                    "text/html;charset=UTF-8");

            // 发送邮件
            Transport.send(message);
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    @Deprecated
    public static void sendMail(String target,String title,String content){
        try{
            InputStream inputStream=MailUtil.class.getClassLoader().getResourceAsStream("global.properties");
            final Properties properties=new Properties();
            properties.load(inputStream);
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator=new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = properties.getProperty("mail.user");
                    String password = properties.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            InternetAddress form = new InternetAddress(
                    properties.getProperty("mail.user"));
            message.setFrom(form);

            // 设置收件人
            String[] address=target.split(",");
            for(String addr:address) {
                message.addRecipients(RecipientType.TO,addr);
            }
            if(!target.contains("zy134134@126.com")){
                // 设置密送，其他的收件人不能看到密送的邮件地址
                InternetAddress bcc = new InternetAddress("zy134134@126.com");
                message.setRecipient(RecipientType.CC, bcc);
            }
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(content+"<br>"
                            +"FROM："+properties.getProperty("app.code")+" "+properties.getProperty("app.version")+"<br>"
                            +DateUtil.getNow(DateUtil.DEFAULT_DATETIME_FORMAT),
                    "text/html;charset=UTF-8");

            // 发送邮件
            Transport.send(message);
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
